package fr.emn.integration.synthese.presentation;

import java.awt.Dimension;
import java.awt.Frame;
import java.awt.GridLayout;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;

import fr.emn.integration.synthese.abstraction.Cours;
import fr.emn.integration.synthese.abstraction.Groupe;
import fr.emn.integration.synthese.controle.ControlJButtonSaveGroupe;

public class GroupeDialog extends JDialog {

	private static final long serialVersionUID = 1L;

	private JTextField id;
	private JTextField encadrant;

	public int getId() {
		try {
			int i = Integer.parseInt(id.getText());
			if (i<0) {
				JOptionPane.showMessageDialog(this, "On ne peut pas!", "Erreur!", JOptionPane.ERROR_MESSAGE);
				i = -1;
			}
			return i;
		} catch (NumberFormatException e) {
			JOptionPane.showMessageDialog(this, "On ne peut pas!", "Erreur!",
					JOptionPane.ERROR_MESSAGE);
			return -1;
		}
	}

	public String getEncadrant() {
		return encadrant.getText();
	}

	public GroupeDialog(Frame parent, Cours modele, boolean creation) {
		super(parent, "Groupe", true);
		this.setMinimumSize(new Dimension(200, 100));

		this.getContentPane().setLayout(new GridLayout(1, 3));

		JPanel form = new JPanel(new GridLayout(5, 1));
		form.setBorder(BorderFactory.createEmptyBorder(6, 6, 6, 6));

		form.add(new JLabel("ID"));
		id = new JTextField();
		form.add(id);
		form.add(new JLabel("Encadrant"));
		encadrant = new JTextField();
		form.add(encadrant);
		JPanel buttons = new JPanel();
		JButton save = new JButton("OK");
		save.addActionListener(new ControlJButtonSaveGroupe(this, modele,
				creation));
		buttons.add(save);
		form.add(buttons);

		this.getContentPane().add(form);

		if (!creation) {
			Groupe courante = modele.findGroupe(modele.getModuleCourante(),
					modele.getGroupeCourante());
			id.setText("" + courante.getId());
			encadrant.setText(courante.getEncadrant());
		}

		this.pack();
		super.setLocationRelativeTo(parent);
	}

}
